package com.lxg.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.lxg.dao.BookDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

/**
 * @author 陆小根
 * date: 2022/04/24
 * Description:
 */

//@Configuration
public class JdbcConfig {
  @Value("com.mysql.cj.jdbc.Driver")
  private String driver;
  @Value("jdbc:mysql://localhost:3306/spring_db")
  private String url;
  @Value("root")
  private String username;
  @Value("root")
  private String password;


  // 1.定义一个方法获取要管理的对象
  // 2.添加@Bean，表示当前方法的返回值是一个bean
  @Bean
  public DataSource dataSource(BookDao bookDao) {
    System.out.println(bookDao); // 自动装配
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setDriverClassName(driver);
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    return dataSource;
  }

}
